package jsp;

import util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("*.cookie")
public class CookieServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        switch (path) {
            case "/add.cookie":
                addCookie(req, resp);
                break;
            case "/find.cookie":
                findCookie(req, resp);
                break;
            case "/modify.cookie":
                modifyCookie(req, resp);
                break;
            case "/remove.cookie":
                removeCookie(req, resp);
                break;
        }
    }

    public void addCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 创建Cookie对象
//        Cookie cookie = new Cookie("name", "管理员");
//        // 设置cookie生存时间,单位:秒
//        // 当达到生存时间后,删除cookie
//        // 当尚未达到生存时间时,会话结束后仍然存在
//        cookie.setMaxAge(100);
//        Cookie cookie2 = new Cookie("username", "admin");
//        Cookie cookie3 = new Cookie("password", "123456");
//        // 将Cookie信息加入到响应对象,跟随着响应对象返回到客户端
//        resp.addCookie(cookie);
//        resp.addCookie(cookie2);
//        resp.addCookie(cookie3);

        CookieUtil.addCookie("name", "管理员", 100, resp);
        CookieUtil.addCookie("username", "admin", resp);
        CookieUtil.addCookie("password", "123456", resp);
    }

    public void findCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 当客户端访问服务器时,会将客户端中的cookie信息跟随着请求对象传递到服务器中
        // 可以从请求对象中获取所有的Cookie信息
//        Cookie[] cookies = req.getCookies();
//        for (Cookie cookie : cookies) {
//            System.out.println(cookie.getName() + ":" + cookie.getValue());
//        }
        String name = CookieUtil.findByName("name", req);
        String username = CookieUtil.findByName("username", req);
        String password = CookieUtil.findByName("password", req);
        System.out.println("name:" + name);
        System.out.println("username:" + username);
        System.out.println("password:" + password);
    }

    public void modifyCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Cookie没有提供修改方法
        // 但是,当添加一个新的Cookie数据到时候,如果key已经存在了,默认覆盖原来的
        // 通过这种特性实现修改功能
//        Cookie cookie = new Cookie("password", "666666");
//        resp.addCookie(cookie);
        CookieUtil.modifyCookie("password", "666666", resp);
    }

    public void removeCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 当生存时间归零后,自动删除cookie
        // 可以通过手动设置cookie生存时间为0的方式间接的实现删除
//        Cookie cookie = new Cookie("name", "");
//        cookie.setMaxAge(0);
//        resp.addCookie(cookie);
        CookieUtil.removeCookie("name", resp);
    }
}
